package main

import (
	"flag"
	"gitee.com/hxchjm/go-admin/internal/dao"
	"gitee.com/hxchjm/go-admin/internal/server"
	"gitee.com/hxchjm/go-admin/pkg/cas"
	"gitee.com/hxchjm/go-admin/pkg/db"
	"github.com/hxchjm/config"
	"github.com/hxchjm/log"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	flag.Parse()
	log.Init(nil)
	defer log.Close()
	log.SetFormat("[%D %T] [%L] [%s] [%F] %M")
	log.Info("nms start")

	if err := config.Init(); err != nil {
		panic(err)
	}

	dao.Migrate()

	if err := db.Init(); err != nil {
		panic(err)
	}

	if err := cas.Init(); err != nil {
		panic(err)
	}
	server.New().Start(":8001")

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	for {
		s := <-c
		log.Info("get a signal %s", s.String())
		switch s {
		case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
			log.Info("nms exit")
			time.Sleep(time.Second)
			return
		case syscall.SIGHUP:
		default:
			return
		}
	}
}
